//-------------------------------------------------------------------------
// Copyright (c) 2000-2006 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//
// Original author:lxt
//
//-------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UFINITY SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). UFINITY
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
//-------------------------------------------------------------------------

package com.ufinity.estimation.site.controller.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opensymphony.xwork2.ActionSupport;
import com.ufinity.estimation.persistence.Pager;
import com.ufinity.estimation.persistence.domain.FixedPrice;
import com.ufinity.estimation.persistence.domain.MaterialPrice;
import com.ufinity.estimation.site.controller.formbean.FixedPriceBean;
import com.ufinity.estimation.site.controller.formbean.MaterialPriceBean;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

/**
 * 
 * Description of the class
 * 
 * @author lxt
 * @version 1.0
 * @since 2009-5-13
 */
public class ActionTemplate<E> extends ActionSupport implements ServletRequestAware, ServletResponseAware {

	/**
	 * serialVersionUID long
	 */
	private static final long serialVersionUID = 3267118048207738580L;

	// protected UserDetail sessionUser = (UserDetail) SecurityContextHolder
	// .getContext()
	// .getAuthentication()
	// .getPrincipal();

	protected Pager<E> pager;

	protected int currentPage = 0;

	protected transient HttpServletRequest request;

	protected transient HttpServletResponse response;

	public static final String LIST = "list";

	public static final String UPDATE = "update";

	public static final String DELETE = "delete";

	public static final String EDIT = "edit";

	public static final String EDIT_PASSWORD = "editPassword";

	public static final String SAVE = "save";

	public static final String ADD = "add";

	public static final String CLOSE = "close";

	public static final String INDEX = "index";

	public static final String MAIN = "main";

	public static final String REDIRECT_INDEX = "redirect_index";

	public static final String REDIRECT_EDIT = "redirect_edit";

	public static final String MESSAGE = "message";

	public static final String DETAIL = "detail";

	protected String perform = "";

	protected transient Map<String, String> queryCondition = new HashMap<String, String>();

	protected E entity;

	public Map<String, String> getQueryCondition() {
		return queryCondition;
	}

	public void setQueryCondition(Map<String, String> queryCondition) {
		this.queryCondition = queryCondition;
	}

	public E getEntity() {
		return entity;
	}

	public void setEntity(E entity) {
		this.entity = entity;
	}

	public String getPerform() {
		return perform;
	}

	public void setPerform(String perform) {
		this.perform = perform;
	}

	/**
	 * 
	 * wrap request.setAttribute() method
	 * 
	 * @since 2010-8-6
	 * @param key the attribute name
	 * @param value the attribute value
	 */
	protected void addAttribute(String key, Object value) {
		request.setAttribute(key, value);
	}

	/**
	 * 
	 * iterator map, then put all key and value into request Object.
	 * 
	 * @since 2010-8-6
	 * @param params attribute map
	 */
	protected void addAttributes(Map<String, Object> params) {
		if (params instanceof Map) {
			Iterator<Map.Entry<String, Object>> paramsEntries = params.entrySet().iterator();
			while (paramsEntries.hasNext()) {
				Map.Entry<String, Object> entry = paramsEntries.next();
				request.setAttribute(entry.getKey(), entry.getValue());
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @since 2010-5-25
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	/**
	 * {@inheritDoc}
	 * 
	 * @since 2010-5-25
	 * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
	 */
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;

	}

	public Pager<E> getPager() {
		return pager;
	}

	public void setPager(Pager<E> pager) {
		this.pager = pager;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String add() {
		return "add";
	}

	public FixedPriceBean convertFixedPriceBean(FixedPrice entity) {
		FixedPriceBean fixedPriceBean = new FixedPriceBean();
		fixedPriceBean.setExternalPrice(String.valueOf(entity.getExternalPrice()));
		fixedPriceBean.setManCost(String.valueOf(entity.getManCost()));
		fixedPriceBean.setManPrice(String.valueOf(entity.getManPrice()));
		fixedPriceBean.setMaterialAnalyse(entity.getMaterialAnalyse());
		fixedPriceBean.setMaterialCost(String.valueOf(entity.getMaterialCost()));
		fixedPriceBean.setMaterialPrice(String.valueOf(entity.getMaterialPrice()));
		fixedPriceBean.setName(entity.getName());
		fixedPriceBean.setUnit(entity.getUnit());
		fixedPriceBean.setWorkmanship(entity.getWorkmanship());
		fixedPriceBean.setId(entity.getId());
		fixedPriceBean.setFixedTypeId(entity.getFixedTypeId());
		fixedPriceBean.setCost(String.valueOf(entity.getMaterialCost() + entity.getManCost()));
		fixedPriceBean.setPrice(String.valueOf(entity.getMaterialPrice() + entity.getManPrice()));

		return fixedPriceBean;
	}

	public List<FixedPriceBean> convertPriceList(List<FixedPrice> fixedPriceList) {
		List<FixedPriceBean> fixedPriceBeanList = new ArrayList<FixedPriceBean>();
		for (int i = 0; i < fixedPriceList.size(); i++) {
			FixedPrice fixedPrice = fixedPriceList.get(i);
			FixedPriceBean fixedPriceBean = convertFixedPriceBean(fixedPrice);
			fixedPriceBeanList.add(fixedPriceBean);
		}
		return fixedPriceBeanList;

	}

	public MaterialPriceBean convertMaterialPriceBean(MaterialPrice entity) {
		MaterialPriceBean materialPriceBean = new MaterialPriceBean();
		materialPriceBean.setBrand(entity.getBrand());
		materialPriceBean.setCost(String.valueOf(entity.getCost()));
		materialPriceBean.setGrade(entity.getGrade());
		materialPriceBean.setMadeIn(entity.getMadeIn());
		materialPriceBean.setName(entity.getName());
		materialPriceBean.setRemark(entity.getRemark());
		materialPriceBean.setSpecification(entity.getSpecification());
		materialPriceBean.setSuggestPrice(String.valueOf(entity.getSuggestPrice()));
		materialPriceBean.setUnit(entity.getUnit());
		materialPriceBean.setId(entity.getId());
		materialPriceBean.setFixedTypeId(entity.getFixedTypeId());
		return materialPriceBean;

	}

	public List<MaterialPriceBean> convertMaterialPriceList(List<MaterialPrice> materialPricList) {
		List<MaterialPriceBean> materialPriceBeanList = new ArrayList<MaterialPriceBean>();
		for (int i = 0; i < materialPricList.size(); i++) {
			MaterialPrice materialPrice = materialPricList.get(i);
			MaterialPriceBean materialPriceBean = convertMaterialPriceBean(materialPrice);
			materialPriceBeanList.add(materialPriceBean);
		}
		return materialPriceBeanList;
	}

	@Override
	public void validate() {
		boolean r = (Boolean) request.getSession().getServletContext().getAttribute("r");

		if (!r && !request.getRequestURI().endsWith("register.html")) {
			try {
				response.sendRedirect(request.getContextPath() + "/register.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
